<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 07.12.11
 * Time: 2:43
 * To change this template use File | Settings | File Templates.
 */

class courseController extends Core_BaseController
{

    function indexAction($idCourse = 0)
    {
        $permission = array('guest','admin');
        $this->twig = $this->getRegistry()->twig;
        $allow = $this->userHasPermission($permission);
        $role = $this->getAuth()->getUserRoles();
        if ($allow) {
            $authorized = $this->checkAuth();
            $courseManager = new Course_Course();
            $lessonManager = new Lesson_Lesson();
            $userManager = new User_Manager();

            $userProfile = $userManager->getUserIdProfileById($_COOKIE['id']);
            $completed = $lessonManager->getCompletedLesson($userProfile);
            $this->twig = $this->getRegistry()->twig;
            if ($idCourse < 0) $idCourse = 0;

            $nameCourse = $courseManager->getCourseNameByID($idCourse);
            $lessons = $lessonManager->getAllLessonsByCourse($idCourse);

            for ($i = 0; $i < count($lessons); $i++) {
                $lessonId = $lessons[$i]['idlesson'];
                if (count(array_intersect(array($lessonId), $completed)) != 0) {
                    $lessons[$i]['completed'] = true;
                } else
                    $lessons[$i]['completed'] = false;
            }


            $data = array('idcourse' => $idCourse,
                          'coursename' => $nameCourse,
                          'lessons' => $lessons,
                           'role' => $role);
            $data = array_merge($data, $authorized);
            $role = $this->getAuth()->getUserRoles($authorized);
            $template = $this->twig->loadTemplate('course_index.html.twig');
            echo $template->render($data);

        }
        else {
            $template = $this->twig->loadTemplate('access.html.twig');
            echo $template->render(array());
        }
    }
}
